// pages/firstSetting/firstSetting.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    navBarHeight: app.globalData.navBarHeight, //导航栏高度
    menuBotton: app.globalData.menuBotton, //导航栏距离顶部距离
    menuHeight: app.globalData.menuHeight, //导航栏高度
    current: 0,
    maleSrc: '../../images/man.png',
    femaleSrc: '../../images/woman1.png',
    selectValue: '请选择出生日期',
    infoData: [{
      title: "性别选择",
      hint: "我们需要知道您的性别来给您定制计划"
    }, {
      title: "年龄选择",
      hint: "我们需要知道您的年龄来给您推荐食谱"
    }, {
      title: "您的身高？",
      hint: "我们需要知道您的身高体重来确定您的饮食"
    }, {
      title: "您的口味习惯（可多选）",
      hint: ""
    }],
    flavorData: [{
        title: "川菜",
        active: false
      },
      {
        title: "鲁菜",
        active: false
      },
      {
        title: "粤菜",
        active: false
      },
      {
        title: "苏菜",
        active: false
      },
      {
        title: "浙菜",
        active: false
      },
      {
        title: "闽菜",
        active: false
      },
      {
        title: "湘菜",
        active: false
      },
      {
        title: "徽菜",
        active: false
      },
      {
        title: "酸",
        active: false
      },
      {
        title: "甜",
        active: false
      },
      {
        title: "苦",
        active: false
      },
      {
        title: "辣",
        active: false
      },
      {
        title: "咸",
        active: false
      },
      {
        title: "鲜",
        active: false
      },
      {
        title: "涩",
        active: false
      },
      {
        title: "麻",
        active: false
      },
    ],
    otherActive: false,
    userFlavor:'',
    height: 0,
    weight: 0,
    gender:0,
  },
  change: function (e) {
    console.log(e.currentTarget.dataset.v);
    var v = e.currentTarget.dataset.v
    if (v == "男") {
      //改变男图片
      this.setData({
        maleSrc: '../../images/man1.png',
        femaleSrc: '../../images/woman.png',
        gender:1
      })
    } else {
      this.setData({
        maleSrc: '../../images/man.png',
        femaleSrc: '../../images/woman1.png',
        gender:0
      })
    }
  },
  bindDateChange: function (e) {
    console.log(e.detail.value);
    this.setData({
      selectValue: e.detail.value
    })
  },
  selectFlavor: function (e) {
    console.log(e.currentTarget.dataset.i);
    var i = e.currentTarget.dataset.i
    if (i == -1) {
      var otherActive = !this.data.otherActive
      this.setData({
        otherActive,
        userFlavor:this.data.userFlavor+='其他#'
      })
      return
    }
    var d = this.data.flavorData
    d[i].active = !d[i].active
    console.log(d);
    this.setData({
      flavorData: d,
      userFlavor:this.data.userFlavor+=(d[i].title+'#')
    })
  },
  getBodyV: function (e) {
    console.log(e.currentTarget.dataset.type);
    var t = e.currentTarget.dataset.type
    if (t == 'h') {
      this.setData({
        height: e.detail.value
      })
    } else {
      this.setData({
        weight: e.detail.value
      })
    }
  },
  next: function (e) {
    var idx = this.data.current

    if (idx == this.data.infoData.length - 1) { //==3 口味选择

      var flag = false
      for (var i = 0; i < this.data.flavorData.length; i++) {
        if (this.data.flavorData[i].active) {
          flag = true
          break
        }
      }
      if (this.data.otherActive) flag = true
      if (!flag) {
        wx.showToast({
          title: '请选择一个口味！',
          icon: "error"
        })

        return
      }
      wx.navigateTo({
        url: '../biaodan/biaodan?t=f&height='+this.data.height+'&weight='+this.data.weight+"&birth="+this.data.selectValue+"&flavor="+this.data.userFlavor+"&gender="+this.data.gender,
      })
      return
    }
    if (idx == 1 && this.data.selectValue == "请选择出生日期") {
      wx.showToast({
        title: '请选择出生日期',
        icon: "error"
      })
      return
    }
    if (idx == 2 && (this.data.height == 0 || this.data.weight == 0)) {
      wx.showToast({
        title: '请输入完整信息',
        icon: "error"
      })
      return
    }
    idx++
    console.log(idx);
    this.setData({
      current: idx
    })
  },
  back: function (e) {
    console.log(e);
    var idx = this.data.current
    if (idx == 0) {
    wx.showModal({
      cancelColor: 'cancelColor',
      title:'确定要退出吗?',
      content:'为您更好的体验,我们需要您的数据!',
      success:(res)=>{
        if(res.confirm){
          wx.switchTab({
            url: '../index/index',
          })
        }
      }
    })
      return
    }
    idx--
    this.setData({
      current: idx
    })
  },
  jump:function(e){
    console.log('跳过');
    wx.navigateTo({
      url: "../biaodan/biaodan"
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    //  this.setData({
    //   today:new Date().getDate()
    //  })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})